package metier;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import dao.EnseignementDAO;


public class Enseignement {
	private int id = -1;
	private String intitule;
	private String description;
	private boolean _builtFromDB;
	private Departement dep;
	private EnseignementDAO dao;

	public Enseignement() {
		_builtFromDB=false;
	}

	public Enseignement(String intitule, Departement dep) {
		this._builtFromDB = false;
		this.intitule = intitule;
		this.dep = dep;
	}

	public boolean is_builtFromDB() {
		return _builtFromDB;
	}

	public void set_builtFromDB(boolean fromDB) {
		_builtFromDB = fromDB;
	}

	public Departement getDep() {
		return dep;
	}

	public void setDep(Departement dep) {
		this.dep = dep;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getIntitule() {
		return intitule;
	}

	public void setIntitule(String intitule) {
		this.intitule = intitule;
	}

	public boolean equals(Object other) { 
		if (this == other) return true; 
		if ( !(other instanceof Enseignement) ) return false;  
		final Enseignement obj = (Enseignement) other; 
		if ( obj.getId()!=getId() ) 
			return false;           
		return true; 
	}

	public int hashCode(){
		return id * 39 ;
	}
	
	public void save() throws IOException, ClassNotFoundException, SQLException, Exception {
		dao = EnseignementDAO.getDAO();
		dao.update(this);
	}
	
	public void delete() throws IOException, ClassNotFoundException, SQLException, Exception {
		dao = EnseignementDAO.getDAO();
		dao.deleteByID(this.id, this.dep.getId());
	}
	
	public static ArrayList<Enseignement> load() throws IOException, ClassNotFoundException, SQLException, Exception{
		EnseignementDAO myDao = EnseignementDAO.getDAO();
		return myDao.getEnseignements();
	}
	
	public String toString(){
		return "Enseignement : " + intitule + " propose au " + dep;
	}
}
